package com.zfk.transaction.controller;

import com.alibaba.fastjson.JSON;
import com.zfk.transaction.entity.Location;
import com.zfk.transaction.service.LocationService;
import com.zfk.transaction.util.Response;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.jws.Oneway;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/location")
@Api("LocationController")
public class LocationController {

    @Autowired
    private LocationService service;


    @GetMapping("/getAll")
    @ApiOperation("获得该用户的所有地址数据")
    public Response getDynamic(@RequestParam int user_id){
        System.out.println("user_id"+user_id);
        List<Location> locations = service.getLocations(user_id);
        return Response.ok(locations);
    }


    @PostMapping("/addLocation")
    @ApiOperation("新增地址")
    public Response addDynamic(@RequestBody Map<String,Object> jsonString){
        // 输入的地址对象
        Object locationJSON = jsonString.get("location");
        System.out.println(locationJSON);
        String string = JSON.toJSONString(locationJSON);

        Map<String, Object> map = JSON.parseObject(string, Map.class);
        System.out.println("map = " + map);

        Location location = new Location((Integer) map.get("id"),(String) map.get("userName"),(String)map.get("phone"),(String)map.get("location"),(Integer) map.get("userId"));

        int result = service.addLocation(location);
        return Response.ok();
    }

    @PostMapping("/updateLocation")
    @ApiOperation("修改地址")
    public Response updateDynamic(@RequestBody Map<String,Object> jsonString){
        // 输入的地址对象
        Object locationJSON = jsonString.get("location");

        String string = JSON.toJSONString(locationJSON);

        Map<String, Object> map = JSON.parseObject(string, Map.class);
        System.out.println("map = " + map);

        Location location = new Location((Integer) map.get("id"),(String) map.get("userName"),(String)map.get("phone"),(String)map.get("location"),(Integer) map.get("userId"));
        int result = service.updateLocation(location);
        return Response.ok();
    }

    @GetMapping("/deleteLocation")
    @ApiOperation("删除地址")
    public Response deleteData(@RequestParam int id){
        service.deleteLocation(id);
        return Response.ok();
    }


}
